$(function() {

});

// 查询新闻详情
function showNewsDetail(newsId, type) {
//	window.open("newsCenterAction_queryNewsInfoDetail.action?newsInfo.newsId="
//			+ newsId + "&newsInfo.type=" + type + "&random=" + Math.random(),
//			"_blank");
	
	window.open(ctx_root + "/newsInfo/"+newsId+"_"+type+".html","_blank");

}

function formateDate(date){
	var df=new SimpleDateFormat();//jsJava1.0需要使用DateFormat对象，不要弄错就是了 
	df.applyPattern("yyyy-MM-dd HH:mm:ss"); 
	var str=df.format(date); 
	return str;
}

// 页面初始化时加载评论信息
function commentListInit(url, pageNow, pageSize) {
	$("#commentsList").html("");
	$("#pageDivId").remove();
	$("#pageSign").val(pageNow);
	if(url==null){
		url="newsCenterAction_getCommentList.action";
	}
	var newsId = $("#newsId").val();
	$.post(url,
		{
			"pageNo" : pageNow,
			"pageSize" : pageSize,
			"newsInfo.newsId" : newsId,
			"newsInfo.type" : $("#type").val()
		},
		function(data) {
			// 拼页面
			var html = new Array();
			var strPage="";
			html.push("<div class='t'><span>最新评论</span><a href='javascript:shuaxin();' class='btn_greyb l'>刷新</a></div>");
			if (data.commentPage == null) {
				html.push("<p class='no_comment'>暂无评论</p>");
			} else {
				//var size1 = data.commentPage.records.length;
				$.each(data.commentPage.records,function(i, comment) {
					var approve = comment.approve == null ? 0: comment.approve; // 点赞次数
					var revertTimes = comment.revertTimes == null ? 0: comment.revertTimes; // 回复次数
					var text = "<dl class='recent_com_list clearfix'>"
							+ "<dt><img src='/images/user_defalt.gif' width='60' height='60' alt='用户默认头像'></dt>"
							+ "<dd><p class='name_date clearfix'>"
							+ "<span class='name'>"+ comment.userName+ "</span>"
							+ "<span class='date'>发表日期："+ comment.commentTimeStr+ "</span></p>"
							+ "<p class='comm_con' style='word-wrap: break-word; word-break: break-all;'>"+ comment.words+ "</p>"
							+ "<p class='comm_acti'>"
							+ "<a id='"+ comment.commentId+ "_aDiv' href='javascript:addCommentPraise("
							+ comment.commentId+ ")'><i>赞</i> <span id='"+ comment.commentId+ "_zan'>["
							+ approve+ "]</span></a> "
							+ "| <a href='javascript:void(0);' onclick='Initaddrevertdiv("+ comment.commentId+ ")'>回复</a>"
							+ "<span style='cursor: pointer;' onclick='revertListInit("+ revertTimes+ ","+ comment.commentId+ ");'>("+ revertTimes+ ")</span></p>"
							+ "<div class='comm_repet' style='display:none;' id='"+ comment.commentId+ "_bigdiv'>"
							+ "<div class='repet_textarea clearfix'style='display:none;' id='"+ comment.commentId+ "_addrevertdiv'></div>"
							+ "<div id='"+ comment.commentId+ "_revertdatadiv' style='display:none;'></div>"
							+ "</div></dd></dl>";
							
					html.push(text);
				});

				strPage = linkedPage(data.commentPage.totalPage,data.commentPage.currentPage,pageSize);

			}
			$("#commentsList").html(html.join(""));

			$(".users_comments").append(strPage);

		});
}



// 获取回复列表
function revertListInit(reverCount, commentId) {
	if(reverCount == null || reverCount == 0){
		return;
	} else {
		if (!$("#" + commentId + "_revertdatadiv").is(":hidden")) {
			$("#"+commentId+"_bigdiv").hide();
			$("#" + commentId + "_revertdatadiv").hide();
		} else{
			var newsId = $("#newsId").val();

			$.post("newsCenterAction_getCommentRevertList.action", {
				"newsInfo.newsId" : newsId,
				"newsInfo.type" : $("#type").val(),
				"comment.commentId" : commentId
			}, function(data) {
				if (data.commentRevertList == null) {
					return;
				} else {
					var html = new Array();
					$.each(data.commentRevertList, function(i, record) {
						var text = "<div class='comm_repet_list'>"
								+ "  <p class='name_date clearfix'>"
								+ "    <span class='name'>"+ record.revertUserName + "</span>"
								+ "    <span class='date'>发表日期："+ record.revertTimeStr + "</span>" + "  </p>"
								+ "  <p class='comm_con' style='word-wrap: break-word; word-break: break-all;'>" + record.words+ "</p>" 
								+ "</div>";
						html.push(text);
					});
					$("#" + commentId + "_bigdiv").show();
					$("#" + commentId + "_arrow").show();
					$("#" + commentId + "_revertdatadiv").html(html.join(""));
					$("#" + commentId + "_revertdatadiv").show();
				}
			});
		}
	}
}


// 评论点赞
function addCommentPraise(commentId) {
	var newsId = $("#newsId").val();
	if (this.ajaxRequest_ != undefined && this.ajaxRequest_.readyState < 4) {
        return;
	}else{
		this.ajaxRequest_=$.post("newsCenterAction_addCommentPraise.action", 
		{
			"newsInfo.newsId" : newsId,
			"newsInfo.type" : $("#type").val(),
			commentId : commentId
		}, function(data) {
			var type = data.split("_")[1];
			var statnum = data.split("_")[0];
			if (type == "errorNum" && statnum == 1) {// 错误信息
	
			} else if (type == "resNum" && statnum > 0) {
				$("#" + commentId + "_aDiv").attr("href", "javascript:void(0);");
				$("#" + commentId + "_aDiv").attr("title", "已赞");
				$("#" + commentId + "_aDiv").attr("style", "color:gray;");
				$("#" + commentId + "_zan").html("[" + statnum + "]");
			}
		});
	}
	
}

// 文章点赞
function addNewsPraise(newsId, type) {
	if (this.ajaxRequest_ != undefined && this.ajaxRequest_.readyState < 4) {
        return;
	}else{
		this.ajaxRequest_=$.post("newsCenterAction_addNewsPraise.action", 
			{
				"newsInfo.newsId" : newsId,
				"newsInfo.type" : type
			}, function(date) {
				var type = date.split("_")[1];
				var statnum = date.split("_")[0];
		
				if (type == "errorNum" && statnum == 1) {// 错误信息
		
				} else if (type == "resNum" && statnum > 0) {
					$("#" + newsId + "_zan").html("<i class='zan_icon'></i><span class='zan_views'>"+ statnum + "</span>");
					$("#" + newsId + "_zan").attr('href', "javascript:void(0);");
					$("#" + newsId + "_zan").attr('title', "已赞");
					$("#" + newsId + "_zan").attr('style', "color:gray;");
					$("#" + newsId + "_zan").attr('class', "zan_has");
				}
			});
	}
}

// js获取主机地址，如： http://localhost:8080
function getHostPath() {
	// 获取当前网址，如： http://localhost:8083/uimcardprj/share/meun.jsp
	var curWwwPath = window.document.location.href;
	// 获取主机地址之后的目录，如： uimcardprj/share/meun.jsp
	var pathName = window.document.location.pathname;
	var pos = curWwwPath.indexOf(pathName);
	// 获取主机地址，如： http://localhost:8083
	var localhostPath = curWwwPath.substring(0, pos);
	// 获取带"/"的项目名，如：/uimcardprj
	// var projectName=pathName.substring(0,pathName.substr(1).indexOf('/')+1);
	return (localhostPath);
}

// 点击回复弹出回复框
//function Initaddrevertdiv(commentId) {
//	if ($("#" + commentId + "_addrevertdiv").html() == "") {// 显示回复框
//		$("#" + commentId + "_bigdiv").show();
//		var testarea = "<textarea name='' cols='' rows='' id='"
//				+ commentId
//				+ "_revert'></textarea><div class='clearfix'><a href='javascript:void(0);' class='btn_red right' onclick='addrevert("
//				+ commentId
//				+ ",this)'>回复</a><p class='right' id='"
//				+ commentId
//				+ "_addrevertError'></p></div>"
//				+ "<div class='submit' style='display:none;' id='"
//				+ commentId
//				+ "_afteraddrevert'><a href='javascript:void(0);' class='btn_red' style='float:right;'>回复</a><span class='right'>正在提交中...</span><img src='"
//				+ baseAddress
//				+ "images/icon_loading.gif' width='16' height='16' alt='loading' class='right'></div>";
//		$("#" + commentId + "_addrevertdiv").html(testarea);
//		$("#" + commentId + "_addrevertdiv").show();
//		$("#" + commentId + "_arrow").show();
//	} else if ($("#" + commentId + "_revertdatadiv").html() != "") {
//		$("#" + commentId + "_bigdiv").show();
//		$("#" + commentId + "_arrow").show();
//		$("#" + commentId + "_addrevertdiv").html("");
//		$("#" + commentId + "_addrevertdiv").hide();
//	} else {// 回复框已经显示时，隐藏掉
//		$("#" + commentId + "_addrevertdiv").html("");
//		$("#" + commentId + "_addrevertdiv").hide();
//		$("#" + commentId + "_arrow").hide();
//		$("#" + commentId + "_bigdiv").hide();
//	}
//}


//点击回复弹出回复框
function Initaddrevertdiv(commentId){
		$(".repet_textarea").hide();
		$(".comm_repet").hide();
	if($("#"+commentId+"_addrevertdiv").is(":hidden")){
		$("#"+commentId+"_bigdiv").show();
		 $("#"+commentId+"_addrevertdiv").show();
		 var strHtml="<textarea name='' id='"+commentId+"commentWords' cols='' rows=''></textarea><div class='clearfix'><a href='javascript:addCommentRevert("+commentId+");' class='btn_red r'>回复</a></div>";
		 $("#"+commentId+"_addrevertdiv").html(strHtml);
	}else{
		$("#"+commentId+"_bigdiv").hide();
		$("#"+commentId+"_addrevertdiv").hide();
	}
}

//评论列表刷新
function shuaxin(){
	commentListInit("newsCenterAction_getCommentList.action",1,10);
}



